/**
 * @author Jason Pratt, Dennis Cosgrove
 */

package edu.cmu.cs.stage3.alice.authoringtool.dialog;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;
import edu.cmu.cs.stage3.alice.core.Element;

public class SimulationExceptionPanel extends javax.swing.JPanel {

 private AuthoringTool m_authoringTool;

 private javax.swing.JLabel m_descriptionLabel = new javax.swing.JLabel();

 public SimulationExceptionPanel( AuthoringTool authoringTool ) {
  m_authoringTool = authoringTool;

  setLayout( new java.awt.GridBagLayout() );

  java.awt.GridBagConstraints gbc = new java.awt.GridBagConstraints();
  gbc.anchor = java.awt.GridBagConstraints.NORTHWEST;
  gbc.fill = java.awt.GridBagConstraints.BOTH;
  gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;

  add( new javax.swing.JLabel( "iVProg has detected a problem with your world:" ), gbc );
  gbc.insets.left = 8;
  add( m_descriptionLabel, gbc );
  //add( m_stackPanel, gbc );
 }

 public void setSimulationException( edu.cmu.cs.stage3.alice.core.SimulationException simulationException ) {
  m_descriptionLabel.setText( simulationException.getMessage() );

  edu.cmu.cs.stage3.alice.core.Element element = simulationException.getElement();
  edu.cmu.cs.stage3.alice.core.World world = m_authoringTool.getWorld();
  edu.cmu.cs.stage3.alice.core.Element ancestor = null;
  edu.cmu.cs.stage3.alice.core.Element[] userDefinedResponses = world.getDescendants( edu.cmu.cs.stage3.alice.core.response.UserDefinedResponse.class );
  //LOB for (Element userDefinedResponse : userDefinedResponses)
  int t_tam = userDefinedResponses!=null ? userDefinedResponses.length : 0;
  for (int i_i=0; i_i<t_tam; i_i++) {
   if( userDefinedResponses[i_i].isAncestorOf( element ) ) { //L userDefinedResponse.isAncestorOf( element )
    ancestor = userDefinedResponses[i_i]; //L userDefinedResponse
    break;
   }
  }
  if( ancestor == null ) {
   edu.cmu.cs.stage3.alice.core.Element[] userDefinedQuestions = world.getDescendants( edu.cmu.cs.stage3.alice.core.question.userdefined.Component.class );
   //L for (Element userDefinedQuestion : userDefinedQuestions)
   t_tam = userDefinedQuestions!=null ? userDefinedQuestions.length : 0;
   for (int i_i=0; i_i<t_tam; i_i++) {
    if( userDefinedQuestions[i_i].isAncestorOf( element ) ) { //L userDefinedQuestion.isAncestorOf( element )
     ancestor = userDefinedQuestions[i_i]; //L userDefinedQuestion
     break;
    }
   }
  }

 }


}
